package com.ss.ttvideoengine.c;

import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class OkHttpRequest extends HttpRequest {
    private static OkHttpClient sOkHttpClient = null;
    private Call mCall;

    public OkHttpRequest() {

    }

    public final void cancel() {
        if (this.mCall != null && !this.mCall.isCanceled()) {
            this.mCall.cancel();
        }
    }

    public final void request(String url, Callback callback) {
        synchronized (OkHttpRequest.class) {
            if (OkHttpRequest.sOkHttpClient == null) {
                OkHttpRequest.sOkHttpClient = new OkHttpClient()
                        .newBuilder()
                        .connectTimeout(10, TimeUnit.SECONDS)
                        .writeTimeout(10, TimeUnit.SECONDS)
                        .readTimeout(10, TimeUnit.SECONDS)
                        .build();
            }
        }

        this.mCall = OkHttpRequest.sOkHttpClient.newCall(
                new Request.Builder()
                        .url(url)
                        .build());
        this.mCall.enqueue(new okhttp3.Callback(callback));
    }
}

